package com.deng.permission.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import com.deng.common.SQLHelper;
import com.deng.factory.SF;
import com.deng.permission.vo.LogVo;
import com.deng.permission.dao.ILog;
import com.deng.util.DateTools;

public class LogImplForHibernate extends SQLHelper implements ILog {

	public void addLog(String user_id, String user_name, String module,
			String operation) {
		try
		{
//			System.out.println("1........."+SF.getSF());
//			Session s = SF.getSF().openSession();
//			System.out.println("2........."+s);
//			s.beginTransaction();
//			
//			LogVo lv = new LogVo();
//			lv.setUser_id(user_id);
//			lv.setModule(module);
//			lv.setOperation(operation);
//			lv.setOperation_time(DateTools.dtsWithTime(new Date()));
//			lv.setUser_name(user_name);
//			
//			s.save(lv);
//			s.getTransaction().commit();
//			s.close();
			
//			Connection conn = getConnect();
//			StringBuffer sb = new StringBuffer("insert into t_sys_log (operation_time,module,operation,user_id,user_name)" +
//					" values(");
//			sb.append("'"+DateTools.dtsWithTime(new Date())+"','"+module+"','"+operation+"','"+user_id+"','"+user_name+"')");
//			updateData(conn, sb.toString());
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

	public List<Object> getLogByConditions(String user_name,String user_id, String module,
			String operation, String fromTime, String toTime,int page,int pos) {
		// TODO Auto-generated method stub
		List<Object> rtL = new ArrayList();
		try	{
			Session s = SF.getSF().openSession();
			s.beginTransaction();
			
			List<LogVo> l = new ArrayList<LogVo>();
			StringBuffer sb = new StringBuffer("from LogVo");
			StringBuffer where = new StringBuffer("");
			
			if(user_name != null && !"".equals(user_name))
			{
				where.append(" user_name like '%"+user_name+"%' and");
			}
			if(user_id != null && !"".equals(user_id))
			{
				where.append(" user_id = '"+user_id+"' and");
			}
			if(module != null && !"".equals(module))
			{
				where.append(" module like '%"+module+"%' and");
			}
			if(operation != null && !"".equals(operation))
			{
				where.append(" operation = '"+operation+"' and");
			}
			if(fromTime != null && !"".equals(fromTime))
			{
				where.append(" operation_time >= '"+fromTime+"' and");
			}
			if(toTime != null && !"".equals(toTime))
			{
				where.append(" operation_time < '"+toTime+"' and");
			}
			String tmpSB = "";
			if(where.length() > 0)
			{
				tmpSB = sb.toString()+" where "+where.substring(0, where.length()-3);
				sb = sb.append(" where ").append(where.substring(0, where.length()-3)).append("order by id");
			}
			
			int rows = 26;
			
			Query q = s.createQuery(sb.toString());
			q.setFirstResult((page-1+pos)*rows);
			q.setMaxResults(rows);
			l = q.list();
			
			Query q1 = s.createQuery("select count(id) "+tmpSB);
			List<Long> l2 = q1.list();
			int max = (int)Math.ceil((double)l2.get(0)/(double)rows);
			
			rtL.add(page+pos);
			rtL.add(max);
			rtL.add(l);
			
			s.close();
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		
//		LogVo vo = new LogVo();
//		try
//		{
//			Connection conn = getConnect();
//			StringBuffer sb = new StringBuffer("");
//			StringBuffer sql = new StringBuffer("select * from t_sys_log ");
//			
//			sql.append(" order by operation_time desc");
//			ResultSet rs = queryData(conn, sql.toString());
//			while(rs.next())
//			{
//				vo = new LogVo();
//				vo.id = rs.getInt("id");
//				vo.module = rs.getString("module");
//				vo.operation = rs.getString("operation");
//				vo.operation_time = DateTools.dtsWithTime(rs.getTime("operation_time"));
//				vo.user_id = rs.getString("user_id");
//				vo.user_name = rs.getString("user_name");
//				l.add(vo);
//			}
//		}
//		catch(Exception ex)
//		{
//			ex.printStackTrace();
//		}
		return rtL;
	}

}
